﻿using System.Linq;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;

namespace NazTek.Documenter.Ssis.Control
{
    public class SsisPackage
    {
        internal string Name { get; private set; }
        internal SsisConnectionList Connections { get; set; }

        internal SsisPackage(string pkgLocation)
        {
            var pkg = new Application().LoadPackage(pkgLocation, new SsisEventListener());
            Name = pkg.Name;
            Description = pkg.Description;
            Connections = new SsisConnectionList(pkg.Connections);

            Tasks = new SsisTaskManager(pkg, this);
        }
        internal string GetConnectionName(IDTSComponentMetaData100 metadata)
        {
            return Connections.GetName(
                metadata.RuntimeConnectionCollection.OfType<IDTSRuntimeConnection100>().First().ConnectionManagerID);
        }
        internal string BuildDoc()
        {
            return Tasks.BuildDoc(Title);
        }
        private string Description { get; set; }
        private SsisTaskManager Tasks { get; set; }
        private string Title
        {
            get
            {
                return string.Format("{0} [{1}]", Name,
                    string.IsNullOrWhiteSpace(Description) ? "description unavailable" : Description);
            }
        }
    }
}